package com.wz.web;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

@WebServlet(urlPatterns = {"/checkname"})
public class ajaxServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数
        String username=req.getParameter("username");
        //设置编码
        resp.setContentType("text/html;charset=utf-8");
        //获取内置对象 输出流对象
        PrintWriter writer=resp.getWriter();

        //2.逻辑代码
        if("admin".equals(username)){
            //输入的内容跟设置的一致 表示用户名已被占用
            writer.println("用户名已被占用");
        } else {
            //表示占用
            writer.println("用户名可用");
        }

    }
}
